package com.ruoyi.project.mobile.jiaqin;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.R;
import com.ruoyi.project.jiaqin.answer.domain.JiaqinAnswer;
import com.ruoyi.project.jiaqin.answer.service.IJiaqinAnswerService;
import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import com.ruoyi.project.jiaqin.followcompany.domain.JiaqinFollowCompany;
import com.ruoyi.project.jiaqin.followcompany.service.IJiaqinFollowCompanyService;
import com.ruoyi.project.jiaqin.followmember.domain.JiaqinFollowMember;
import com.ruoyi.project.jiaqin.followmember.service.IJiaqinFollowMemberService;
import com.ruoyi.project.jiaqin.industry.domain.JiaqinIndustry;
import com.ruoyi.project.jiaqin.industry.service.IJiaqinIndustryService;
import com.ruoyi.project.jiaqin.numbertips.domain.NumberTips;
import com.ruoyi.project.jiaqin.numbertips.service.INumberTipsService;
import com.ruoyi.project.jiaqin.question.domain.JiaqinQuestion;
import com.ruoyi.project.jiaqin.question.service.IJiaqinQuestionService;
import com.ruoyi.project.jiaqin.questionlikes.domain.MemberQuestionLikes;
import com.ruoyi.project.jiaqin.questionlikes.service.IMemberQuestionLikesService;
import com.ruoyi.project.jiaqin.subject.domain.JiaqinSubject;
import com.ruoyi.project.jiaqin.subject.service.IJiaqinSubjectService;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/jq/ques")
public class ApiQuestionController {


    @Autowired
    private IJiaqinIndustryService iJiaqinIndustryService;

    @Autowired
    private IJiaqinSubjectService iJiaqinSubjectService;

    @Autowired
    private IJiaqinAnswerService iJiaqinAnswerService;

    @Autowired
    private IJiaqinQuestionService iJiaqinQuestionService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private IMemberQuestionLikesService iMemberQuestionLikesService;
    /**
     * 获取行业列表信息
     * @return
     */
    @GetMapping("/getIndustrys")
    public R getIndustrys(){
        HashMap<String,Object> map = new HashMap<>();
        List<JiaqinIndustry> list = iJiaqinIndustryService.selectJiaqinIndustryList();
        map.put("data",list);
        return R.ok(map);
    }

    /**
     * 根据行业ID获取话题列表
     * @return
     */
    @GetMapping("/getSubjectByIndId")
    public R getSubjectByIndId(@RequestParam(value="pageNum",defaultValue="1",required = false) Integer pageNum
            ,@RequestParam(value="pageSize",defaultValue="5",required = false) Integer pageSize,String industryId){
        HashMap<String,Object> map = new HashMap<>();
        PageInfo<JiaqinSubject> pageInfo = iJiaqinSubjectService.getSubjectByIndId(pageNum,pageSize,industryId);
        map.put("data",pageInfo);
        return R.ok(map);
    }

    /**
     * 发布问题
     * @param jiaqinQuestion
     * @return
     */
    @PostMapping("/releaseQuestion")
    public R releaseQuestion(@RequestBody JiaqinQuestion jiaqinQuestion){
        jiaqinQuestion.setCreateDate(new Date());
        jiaqinQuestion.setAnswers(0L);
        jiaqinQuestion.setViews(0L);
        // 添加我的提问数字提示
        NumberTips numberTips = numberTipsService.selectNumberTipsByMemberId(jiaqinQuestion.getMemberId());
        if (numberTips != null) {
            NumberTips tips = new NumberTips();
            tips.setId(numberTips.getId());
            tips.setQuestionsNum(1);
            numberTipsService.updateNumberTips(tips);
        }
        return iJiaqinQuestionService.insertJiaqinQuestion(jiaqinQuestion)>0?R.ok("发布问题成功"):R.error("发布问题失败");
    }

    /**
     * 回答
     * @param jiaqinAnswer
     * @return
     */
    @PostMapping("/releaseAnswer")
    public R releaseAnswer(@RequestBody JiaqinAnswer jiaqinAnswer){
        jiaqinAnswer.setLikes(0L);
        jiaqinAnswer.setViews(0L);
        jiaqinAnswer.setReleaseDate(new Date());
        return iJiaqinAnswerService.answer(jiaqinAnswer)>0?R.ok("发布回答成功"):R.error("发布回答失败");
    }

    @Autowired
    private INumberTipsService numberTipsService;
    /**
     * 获取提问列表
     * @return
     */
    @GetMapping("/getQuestions")
    public R getQuestions(@RequestParam(value="pageNum",defaultValue="1",required = false) Integer pageNum
            ,@RequestParam(value="pageSize",defaultValue="5",required = false) Integer pageSize
            ,JiaqinQuestion jiaqinQuestion,String userId){
        // 取消我的提问数字提示
        NumberTips numberTips = numberTipsService.selectNumberTipsByMemberId(userId);
        if (numberTips != null) {
            NumberTips tips = new NumberTips();
            tips.setId(numberTips.getId());
            tips.setQuestionsNum(0);
            this.numberTipsService.updateNumberTips(tips);
        }
        HashMap<String,Object> map = new HashMap<>();
        PageInfo<JiaqinQuestion> pageInfo = iJiaqinQuestionService.selectJiaqinQuestions(pageNum,pageSize,jiaqinQuestion,userId);
        map.put("data",pageInfo);
        return R.ok(map);
    }

    @Autowired
    private IMemberService memberService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IJiaqinFollowCompanyService jiaqinFollowCompanyService;
    @Autowired
    private IJiaqinFollowMemberService jiaqinFollowMemberService;
    /**
     * 获取提问详情
     * @return
     */
    @GetMapping("/getQuestionById")
    public R getQuestions(String id,String memberId){
        HashMap<String,Object> map = new HashMap<>();

        JiaqinQuestion jiaqinQuestion = iJiaqinQuestionService.selectJiaqinQuestionById(id);
        if(memberId != null){
            if(iMemberQuestionLikesService.selectMemberQuestionLikesById(memberId,id) == null){
                jiaqinQuestion.setFlagLikes(false);
            }else{
                jiaqinQuestion.setFlagLikes(true);
            }
        }
        // 是否是企业
        if (jiaqinQuestion.getMemberId() != null) {
            Member memberById = memberService.selectMemberById(jiaqinQuestion.getMemberId());
            if (memberById != null) {
                Company company = companyService.selectCompanyByMemberId(jiaqinQuestion.getMemberId());
                if (company != null) {
                    JiaqinFollowCompany followCompany = jiaqinFollowCompanyService.selectJiaqinFollowMemberByMemberIdAndToCompanyId(memberId, company.getId());
                    jiaqinQuestion.setIsCompany(1);// 企业
                    if (followCompany != null) {
                        jiaqinQuestion.setIsCollect(1);
                    } else {
                        jiaqinQuestion.setIsCollect(0);
                    }
                } else {
                    JiaqinFollowMember followMember = jiaqinFollowMemberService.selectJiaqinFollowMemberByMemberIdAndToMemberId(memberId, jiaqinQuestion.getMemberId());
                    jiaqinQuestion.setIsCompany(0);// 个人
                    if (followMember != null) {
                        jiaqinQuestion.setIsCollect(1);
                    } else {
                        jiaqinQuestion.setIsCollect(0);
                    }
                }
            }
        }
        //浏览量+1
        Long numSeeCache = redisTemplate.opsForValue().increment(jiaqinQuestion.getId(),1L);
        jiaqinQuestion.setViews(jiaqinQuestion.getViews()+numSeeCache);
        map.put("data",jiaqinQuestion);
        return R.ok(map);
    }

    /**
     * 用户点赞问题
     * @param userId
     * @param questionId
     * @return
     */
    @GetMapping("/likesQuestion")
    public R likesNews(String userId,String questionId){
        MemberQuestionLikes memberQuestionLikes = iMemberQuestionLikesService.selectMemberQuestionLikesById(userId,questionId);
        if(memberQuestionLikes != null){
            return R.error("点赞失败:已经点过赞了");
        }
        return iJiaqinQuestionService.likesQuestion(userId,questionId)>0?R.ok("点赞成功"):R.error("点赞失败");
    }

    /**
     * 用户取消点赞问题
     * @param userId
     * @param questionId
     * @return
     */
    @GetMapping("/unlikesQuestion")
    public R unlikesNews(String userId,String questionId){
        MemberQuestionLikes memberQuestionLikes = iMemberQuestionLikesService.selectMemberQuestionLikesById(userId,questionId);
        if(memberQuestionLikes == null){
            return R.error("取消失败:还未点赞");
        }
        return iJiaqinQuestionService.unlikesQuestion(userId,questionId)>0?R.ok("取消点赞成功"):R.error("取消点赞失败");
    }


    /**
     * 根据提问ID获取回答列表
     * @return
     */
    @GetMapping("/getAnswerByQuesId")
    public R getQuestions(@RequestParam(value="pageNum",defaultValue="1",required = false) Integer pageNum
            ,@RequestParam(value="pageSize",defaultValue="5",required = false) Integer pageSize,String quesId){
        HashMap<String,Object> map = new HashMap<>();
        PageInfo<JiaqinAnswer> pageInfo = iJiaqinAnswerService.selectJiaqinAnswerByQuesId(pageNum,pageSize,quesId);
        map.put("data",pageInfo);
        return R.ok(map);
    }

}
